// src/utils/request.ts
import axios from 'axios';
// 创建 axios 实例
const request = axios.create({
  baseURL: 'http://47.99.179.156:1314/api', // 真实接口的 baseURL
  timeout: 1000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (res.code !== '0000') {
      // 抛出 Error 对象，便于业务中 try-catch
      return Promise.reject(new Error(res.data ));
    }
    return res; // 返回完整 res，包含 code、msg、data
  },
  (error) => {
    console.error(error.msg || '网络异常');
    return Promise.reject(error);
  }
);

export default request;
